package com.waters321.rock.orm.config.mybatisplus;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.plugins.SqlExplainInterceptor;

@Configuration
public class MybatisPlusPlugin {

  /**
   * 分页插件
   */
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }

  /**
   * 执行分析插件
   */
  @Bean
  @ConfigurationProperties(prefix = "mybatis_plus.custom.sql-explain")
  @ConditionalOnProperty(prefix = "mybatis_plus.custom.sql-explain", name = "enabled",
      matchIfMissing = true)
  public SqlExplainInterceptor SqlExplainInterceptor() {
    return new SqlExplainInterceptor();
  }

  /**
   * SQL执行效率插件
   */
  @Bean
  @ConfigurationProperties(prefix = "mybatis_plus.custom.performance")
  @ConditionalOnProperty(prefix = "mybatis_plus.custom.performance", name = "enabled",
      matchIfMissing = true)
  public PerformanceInterceptor performanceInterceptor() {
    return new PerformanceInterceptor();
  }

  /**
   * 乐观锁插件
   */
  @Bean
  @ConfigurationProperties(prefix = "mybatis_plus.custom.optimistic-locker")
  @ConditionalOnProperty(prefix = "mybatis_plus.custom.optimistic-locker", name = "enabled",
      matchIfMissing = true)
  public OptimisticLockerInterceptor optimisticLockerInterceptor() {
    return new OptimisticLockerInterceptor();
  }



}
